<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      :title="props.title"
      :destroy-on-close="true"
      :width="!props.isReadonly ? '1100px' : '1100px'"
      :align-center="true"
      draggable
      @close="closeDialog"
      style="min-width: 1100px"
      class="maxhDialog"
    >
      <h3 class="mb-[13px] mt-[-10px] c-[#333] text-[15px] font-600 lh-[28px]">
        基本信息
      </h3>

      <el-table
        :data="tableData"
        :span-method="objectSpanMethod"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="row0"
          label="ID"
          align="right"
          width="130"
          class="row0"
        />
        <el-table-column prop="row1" label="Name" />
        <el-table-column
          prop="row2"
          label="Amount 1"
          width="400"
          align="center"
        />
        <el-table-column
          prop="row3"
          label="Amount 2"
          width="130"
          align="right"
        />
        <el-table-column prop="row4" label="Amount 3" />
      </el-table>

      <h3
        v-if="pageList && pageList.length"
        class="mb-[10px] mt-[20px] c-[#333] text-[15px] font-600 lh-[28px]"
      >
        作业项信息
      </h3>

      <el-descriptions
        v-for="item in pageList"
        :key="item.id"
        title=""
        :column="3"
        size="large"
        border
        style="width: 99%; margin-bottom: 20px"
      >
        <el-descriptions-item
          v-if="item.images && item.images.length"
          :span="3"
        >
          <template #label>
            <div class="cell-item">图片</div>
          </template>
          <div style="margin-bottom: -10px">
            <CommonUploadFile
              :showText="false"
              :isRead="props.isReadonly"
              :files="item.images"
            />
          </div>
        </el-descriptions-item>
        <el-descriptions-item :span="2">
          <template #label>
            <div class="cell-item">作业项</div>
          </template>
          <span class="font-600 mr-[20px] text-[15px]">{{
            item.sopItemName || ""
          }}</span>
          <span style="font-size: 12px">{{ item.sopItemCode || "" }}</span>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">作业部位</div>
          </template>
          {{ item.sopItemPosition || "" }}
        </el-descriptions-item>
        <el-descriptions-item :span="2">
          <template #label>
            <div class="cell-item">作业方法</div>
          </template>
          {{ item.sopItemMethod || "" }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">作业工具</div>
          </template>
          {{ item.sopItemTool || "" }}
        </el-descriptions-item>
        <el-descriptions-item :span="2">
          <template #label>
            <div class="cell-item">作业标准</div>
          </template>

          <div style="width: 100%; word-break: break-all">
            {{ item.sopItemStandardValue || "" }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">标准值类型</div>
          </template>
          <el-tag v-if="item.standardValue == 1" type="success">{{
            dictionaryStore.filterDictName("standardValue", item.standardValue)
          }}</el-tag>
          <el-tag v-else>{{
            dictionaryStore.filterDictName("standardValue", item.standardValue)
          }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item v-if="item.standardValue == 2" :span="3">
          <template #label>
            <div class="cell-item">标准值</div>
          </template>
          <div>
            <p
              v-for="(self, index) in item.details"
              :key="index"
              class="mt-[6px] mb-[6px]"
            >
              <span>{{ index + 1 }}、</span>
              <span>{{ self.standardValue }}</span>
              <span v-if="self.resultValue == '1'" class="ml-[24px]"
                ><el-icon color="#67C23A" size="16" style="vertical-align: sub"
                  ><CircleCheckFilled
                /></el-icon>
                正常</span
              >
              <span v-if="self.resultValue == '0'" class="ml-[24px]"
                ><el-icon color="#F56C6C" size="16" style="vertical-align: sub"
                  ><InfoFilled
                /></el-icon>
                异常</span
              >
            </p>
          </div>
        </el-descriptions-item>
        <el-descriptions-item v-if="item.standardValue == 1" :span="3">
          <template #label>
            <div class="cell-item">标准值范围</div>
          </template>
          <span>{{ item.standardMinValue }}{{ item.standardValueUnit }}</span> ~
          <span>{{ item.standardMaxValue }}{{ item.standardValueUnit }}</span>
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import { getStandardTypesApi, getStandardDevicesApi } from "@/utils/commonData";
import { CircleCheckFilled, InfoFilled } from "@element-plus/icons-vue";
import type { TableColumnCtx } from "element-plus";
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "EquWorkStandardDetail",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return true;
    },
  },
  assetKind: {
    type: Number,
    default: null,
  },
});

interface User {
  row0: string;
  row1: string;
  row2: string;
  row3: string;
  row4: string;
}

interface SpanMethodProps {
  row: User;
  column: TableColumnCtx<User>;
  rowIndex: number;
  columnIndex: number;
}

const objectSpanMethod = ({
  row,
  column,
  rowIndex,
  columnIndex,
}: SpanMethodProps) => {
  if (columnIndex === 2) {
    if (rowIndex % 2 === 0) {
      return {
        rowspan: 2,
        colspan: 1,
      };
    } else {
      return {
        rowspan: 0,
        colspan: 0,
      };
    }
  }
};

const tableData: User[] = reactive([
  {
    row0: "作业类型",
    row1: "",
    row2: "name",
    row3: "编制/时间",
    row4: "",
  },
  {
    row0: "资产类型",
    row1: "",
    row2: "",
    row3: "审核/时间",
    row4: "",
  },
  {
    row0: "品牌型号",
    row1: "",
    row2: "code",
    row3: "批准/时间",
    row4: "",
  },
]);

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

watch(
  () => props.data,
  (newVal: any) => {
    if (newVal?.id) {
      const {
        standardName,
        standardCode,
        brandId,
        brandName,
        modelNo,
        standardCompileTime,
        standardExamineTime,
        standardRatifyTime,
        sopTypeName,
        assetTypeName,
      } = newVal;
      tableData[0].row2 = standardName;
      tableData[2].row2 = standardCode;

      tableData[0].row1 = sopTypeName;
      tableData[1].row1 = assetTypeName;
      if (brandName || modelNo) {
        tableData[2].row1 = `${brandName} - ${modelNo}`;
      } else {
        tableData[2].row1 = "";
      }
      tableData[0].row4 = standardCompileTime || "";
      tableData[1].row4 = standardExamineTime || "";
      tableData[2].row4 = standardRatifyTime || "";
      // 获取作业项列表
      getPageList(newVal?.id);
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

// 获取作业类型列表
const typeList = ref(<any>[]);
async function getTypeList(value: string) {
  const params = {
    taskKind: value,
    assetKind: props.assetKind,
  };
  typeList.value = (await getStandardTypesApi(params)) ?? [];
}
getTypeList("");

// 获取作业类型列表
const deviceList = ref(<any>[]);
async function getDeviceList() {
  const params = {
    assetKind: props.assetKind,
  };
  deviceList.value = (await getStandardDevicesApi(params)) ?? [];
}
getDeviceList();

// 获取所有作业项列表
const pageList = ref(<any>[]);
async function getPageList(id: string) {
  const { data } = await request({
    url: "/eam/eamsopitem/getList",
    method: "post",
    data: {
      assetKind: props.assetKind,
      standardId: id,
    },
  }).finally(() => {});
  console.log("pageList data", data);
  pageList.value = data;
}
</script>

<style scoped lang="scss">
:deep(.maxhDialog .el-dialog__body) {
  max-height: 80vh;
  overflow-y: auto;
}
.el-form-item {
  margin-bottom: 15px;
}
:deep(.el-descriptions__label.el-descriptions__cell.is-bordered-label) {
  width: 120px;
}
:deep(.el-descriptions__content.el-descriptions__cell.is-bordered-content) {
  min-width: 180px;
}

:deep(.el-table) {
  font-family: DINLight;
  thead {
    display: none;
  }
  .el-table__row:nth-child(1) {
    td:nth-child(3) {
      font-size: 20px;
      font-weight: 600;
      color: #333;
    }
  }
}
</style>
